package com.tuanzi.loan.business.repository.credithousekeeper.impl;

import com.tuanzi.loan.business.repository.credithousekeeper.CreditCompensatoryChildOrderStatRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.credithousekeeper.CreditCompensatoryChildOrderStatRequest;
import com.tuanzi.loan.vo.response.credithousekeeper.CreditCompensatoryChildOrderStatResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.stat.tables.StatCompensationOrder.STAT_COMPENSATION_ORDER;


/**
 * @param:
 * @Description:    信用卡代偿子订单查询
 * @Author:         xionghao
 * @CreateDate:      2018-08-17
 */
@SuppressWarnings({ "rawtypes" })
@Repository
public class CreditCompensatoryChildOrderStatRepositoryImpl  extends JOOQBaseRepository  implements CreditCompensatoryChildOrderStatRepository{

    @Override
    protected TableImpl getTable() {
        return STAT_COMPENSATION_ORDER;
    }
    @Override
    public Page<CreditCompensatoryChildOrderStatResponse> list(CreditCompensatoryChildOrderStatRequest searchRequest, Pageable pageable) {
        List<String> list = new ArrayList<String>();
        //只选取 待确认，处理中，成功，失败，中止，异常的 状态订单
        list = Arrays.asList(new String[]{"1","10","11","12","13","14","15"});
        fields(STAT_COMPENSATION_ORDER.fields());
        eq(STAT_COMPENSATION_ORDER.ORDERID,searchRequest.getChildOrderNum());
        eq(STAT_COMPENSATION_ORDER.PRODUCT_ID,searchRequest.getProductname());
        eq(STAT_COMPENSATION_ORDER.CHANNEL_FROM,searchRequest.getEntrance());
        eq(STAT_COMPENSATION_ORDER.STATUS,searchRequest.getStatus());
        in(STAT_COMPENSATION_ORDER.STATUS,list);
        ge(STAT_COMPENSATION_ORDER.CTIME,searchRequest.getStartDate());
        le(STAT_COMPENSATION_ORDER.CTIME,searchRequest.getEndDate());
        ge(STAT_COMPENSATION_ORDER.LEFT_LIMIT,searchRequest.getMinamount());
        le(STAT_COMPENSATION_ORDER.LEFT_LIMIT,searchRequest.getMaxamount());
        orderBy(STAT_COMPENSATION_ORDER.CTIME, Sort.Direction.DESC);
        return search(pageable,record -> {
            CreditCompensatoryChildOrderStatResponse response = new CreditCompensatoryChildOrderStatResponse();
            response.setCreatetime(record.get(STAT_COMPENSATION_ORDER.CTIME));
            response.setOrderId(record.get(STAT_COMPENSATION_ORDER.ORDERID));
            response.setProduct(record.get(STAT_COMPENSATION_ORDER.PRODUCT_ID));
            response.setChannel(record.get(STAT_COMPENSATION_ORDER.CHANNEL));
            response.setChildOrdernum(record.get(STAT_COMPENSATION_ORDER.SUCESS_ORDER));
            response.setChildOrderPrice(String.valueOf((Double.valueOf(record.get(STAT_COMPENSATION_ORDER.SUCCESS_AMOUNT))/100)));
            response.setRemainamount(record.get(STAT_COMPENSATION_ORDER.LEFT_LIMIT));
            response.setStatus(record.get(STAT_COMPENSATION_ORDER.STATUS));
            response.setEntrance(record.get(STAT_COMPENSATION_ORDER.CHANNEL_FROM));
            return response;
        });
    }
}
